/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.pacman.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import sg.edu.nus.iss.pacman.constant.Constants;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sg.edu.nus.iss.pacman.dao.PacManDAO;
import sg.edu.nus.iss.pacman.dao.impl.PacManDAOImpl;
import sg.edu.nus.iss.pacman.dao.jb.ViewScoreBean;

/**
 *
 * @author Tabiul Mahmood
 */
public class ViewScoreServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String scoreURL = request.getRequestURI();
        String url = request.getContextPath() + Constants.VIEW_SCORE_URL;
        if (scoreURL.equals(url)) {

            PrintWriter out = response.getWriter();
            response.setContentType("text/html;charset=UTF-8");
            PacManDAO dao = new PacManDAOImpl();
            List<ViewScoreBean> scoreList = dao.getAllScore();

            out.println("<html>");
            out.println("<head><u><Strong>PacMan Top Score List</Strong></u></head>");
            out.println("<body>");
            out.println("<p>");
            if (scoreList == null) {
                out.println("<h1> No Score Found </h1>");
                out.println("</p>");
            } else {
                out.println("<table border='1'>");
                out.println("<tr>");

                out.println("<th>User Name</th>");
                out.println("<th>Map Name</th>");
                out.println("<th>Score</th>");

                out.println("</tr>");

                for (ViewScoreBean score : scoreList) {
                    out.println("<tr>");

                    out.println("<td>" + score.getUsername() + "</td>");
                    out.println("<td>" + score.getMapname() + "</td>");
                    out.println("<td>" + score.getScore() + "</td>");

                    out.println("</tr>");
                }

                out.println(" </table>");
                out.println("</p>");
            }
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    }

        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Returns the Score";
    }// </editor-fold>
}
